home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 97 / CD-ROM 97 / CD-ROM 97.iso / internet / ghostzilla / ghsetup.exe / chrome / pippki.jar / content / pippki / pref-crlupdate.js < prev    next >
Encoding:
Text File  |  2002-04-09  |  8.1 KB  |  261 lines

  1. /*
  2.  * The contents of this file are subject to the Mozilla Public
  3.  * License Version 1.1 (the "License"); you may not use this file
  4.  * except in compliance with the License. You may obtain a copy of
  5.  * the License at http://www.mozilla.org/MPL/
  6.  *
  7.  * Software distributed under the License is distributed on an "AS
  8.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  9.  * implied. See the License for the specific language governing
  10.  * rights and limitations under the License.
  11.  *
  12.  * The Original Code is mozilla.org code.
  13.  *
  14.  * The Initial Developer of the Original Code is Netscape
  15.  * Communications Corporation.  Portions created by Netscape are
  16.  * Copyright (C) 2001 Netscape Communications Corporation. All
  17.  * Rights Reserved.
  18.  *
  19.  * Contributor(s):
  20.  *  Rangan Sen <rangansen@netscape.com>
  21.  */
  22.  
  23. const nsIX509CertDB = Components.interfaces.nsIX509CertDB;
  24. const nsX509CertDB = "@mozilla.org/security/x509certdb;1";
  25. const nsIPKIParamBlock    = Components.interfaces.nsIPKIParamBlock;
  26. const nsIX509Cert         = Components.interfaces.nsIX509Cert;
  27. const nsICrlEntry         = Components.interfaces.nsICrlEntry;
  28. const nsIPref             = Components.interfaces.nsIPref;
  29.  
  30. var crl;
  31. var bundle;
  32. var prefs;
  33. var updateTypeRadio;
  34. var enabledCheckBox;
  35. var timeBasedRadio;
  36. var freqBasedRadio;
  37. var certdb;
  38.  
  39. var autoupdateEnabledString   = "security.crl.autoupdate.enable.";
  40. var autoupdateTimeTypeString  = "security.crl.autoupdate.timingType.";
  41. var autoupdateTimeString      = "security.crl.autoupdate.nextInstant.";
  42. var autoupdateURLString       = "security.crl.autoupdate.url.";
  43. var autoupdateErrCntString    = "security.crl.autoupdate.errCount.";
  44. var autoupdateErrDetailString = "security.crl.autoupdate.errDetail.";
  45. var autoupdateDayCntString    = "security.crl.autoupdate.dayCnt.";
  46. var autoupdateFreqCntString   = "security.crl.autoupdate.freqCnt.";
  47.  
  48. function onLoad()
  49. {
  50.   certdb = Components.classes[nsX509CertDB].getService(nsIX509CertDB);
  51.   var pkiParams = window.arguments[0].QueryInterface(nsIPKIParamBlock);  
  52.   var isupport = pkiParams.getISupportAtIndex(1);
  53.   crl = isupport.QueryInterface(nsICrlEntry);
  54.  
  55.   autoupdateEnabledString    = autoupdateEnabledString + crl.nameInDb;
  56.   autoupdateTimeTypeString  = autoupdateTimeTypeString + crl.nameInDb;
  57.   autoupdateTimeString      = autoupdateTimeString + crl.nameInDb;
  58.   autoupdateDayCntString    = autoupdateDayCntString + crl.nameInDb;
  59.   autoupdateFreqCntString   = autoupdateFreqCntString + crl.nameInDb;
  60.   autoupdateURLString       = autoupdateURLString + crl.nameInDb;
  61.   autoupdateErrCntString    = autoupdateErrCntString + crl.nameInDb;
  62.   autoupdateErrDetailString = autoupdateErrDetailString + crl.nameInDb;
  63.  
  64.   bundle = srGetStrBundle("chrome://pippki/locale/pippki.properties");
  65.   prefs = Components.classes["@mozilla.org/preferences;1"].getService(nsIPref);
  66.  
  67.   updateTypeRadio = document.getElementById("autoUpdateType");
  68.   enabledCheckBox = document.getElementById("enableCheckBox");
  69.   timeBasedRadio = document.getElementById("timeBasedRadio");
  70.   freqBasedRadio = document.getElementById("freqBasedRadio");
  71.  
  72.   //Read the existing prefs, if any
  73.   initializeSelection();
  74.  
  75.   //Set Values for the update url
  76.   updateSelectedURLControls();  
  77. }
  78.  
  79. function updateSelectedTimingControls()
  80. {
  81.   var freqBox = document.getElementById("nextUpdateFreq");
  82.   var timeBox = document.getElementById("nextUpdateDay");
  83.   if(updateTypeRadio.selectedItem.id == "freqBasedRadio"){
  84.     freqBox.removeAttribute("disabled");
  85.     timeBox.disabled = true;
  86.   } else {
  87.     timeBox.removeAttribute("disabled");
  88.     freqBox.disabled = true;
  89.   }
  90. }
  91.  
  92. function initializeSelection()
  93. {
  94.   var menuItemNode;
  95.   var hasAdvertisedURL = false;
  96.   var hasNextUpdate = true;
  97.  
  98.   var lastFetchMenuNode;
  99.   var advertisedMenuNode;
  100.   
  101.   try {
  102.     var isEnabled = prefs.GetBoolPref(autoupdateEnabledString);
  103.     enabledCheckBox.checked = isEnabled;
  104.   } catch(exception){
  105.     enabledCheckBox.checked = false;
  106.   }
  107.  
  108.   //Always the last fetch url, for now.
  109.   var URLDisplayed = document.getElementById("urlName"); 
  110.   URLDisplayed.value = crl.lastFetchURL;
  111.   
  112.   //Decide how many update timing types to be shown
  113.   //If no next update specified, hide the first choice. Default shows both
  114.   if(crl.nextUpdateLocale == null || crl.nextUpdateLocale.length == 0) {
  115.     timeBasedRadio.disabled = true;
  116.     hasNextUpdate = false;
  117.   }
  118.   
  119.   //Set up the initial selections based on defaults and prefs, if any
  120.   try{
  121.     var timingPref = prefs.GetIntPref(autoupdateTimeTypeString);
  122.     if(timingPref != null) {
  123.       if(timingPref == crl.TYPE_AUTOUPDATE_TIME_BASED) {
  124.         if(hasNextUpdate){
  125.           updateTypeRadio.selectedItem = timeBasedRadio;
  126.         }
  127.       } else {
  128.         updateTypeRadio.selectedItem = freqBasedRadio;
  129.       }
  130.     } else {
  131.       if(hasNextUpdate){
  132.         updateTypeRadio.selectedItem = timeBasedRadio;
  133.       } else {
  134.         updateTypeRadio.selectedItem = freqBasedRadio;
  135.       }
  136.     }
  137.     
  138.   }catch(exception){
  139.     if(!hasNextUpdate) {
  140.       updateTypeRadio.selectedItem = freqBasedRadio;
  141.     } else {
  142.       updateTypeRadio.selectedItem = timeBasedRadio;
  143.     }
  144.   }
  145.  
  146.   updateSelectedTimingControls();
  147.  
  148.   //Now, retrieving the day count
  149.   var timeBasedBox = document.getElementById("nextUpdateDay");
  150.   try {
  151.     var dayCnt = prefs.GetCharPref(autoupdateDayCntString);
  152.     //alert(dayCnt);
  153.     if(dayCnt != null){
  154.       timeBasedBox.value = dayCnt;
  155.     } else {
  156.       timeBasedBox.value = 1; 
  157.     }
  158.   } catch(exception) {
  159.     timeBasedBox.value = 1;
  160.   }
  161.  
  162.   var freqBasedBox = document.getElementById("nextUpdateFreq");
  163.   try {
  164.     var freqCnt = prefs.GetCharPref(autoupdateFreqCntString);
  165.     //alert(freqCnt);
  166.     if(freqCnt != null){
  167.       freqBasedBox.value = freqCnt;
  168.     } else {
  169.       freqBasedBox.value = 1; 
  170.     }
  171.   } catch(exception) {
  172.     freqBasedBox.value = 1;
  173.   }
  174.  
  175.   var errorCountText = document.getElementById("FailureCnt");
  176.   var errorDetailsText = document.getElementById("FailureDetails");
  177.   var cnt = 0;
  178.   var text;
  179.   try{
  180.     cnt = prefs.GetIntPref(autoupdateErrCntString);
  181.     txt = prefs.GetCharPref(autoupdateErrDetailString);
  182.   }catch(exception){}
  183.  
  184.   if( cnt > 0 ){
  185.     errorCountText.setAttribute("value",cnt);
  186.     errorDetailsText.setAttribute("value",txt);
  187.   } else {
  188.     errorCountText.setAttribute("value",bundle.GetStringFromName("NoUpdateFailure"));
  189.     var reasonBox = document.getElementById("reasonbox");
  190.     reasonBox.hidden = true;
  191.   }
  192. }
  193.  
  194. function onCancel()
  195. {
  196.   // Close dialog by returning true
  197.   return true;
  198. }
  199.  
  200. function doHelpButton()
  201. {
  202.   openHelp("validation-crl-auto-update-prefs");
  203. }
  204.  
  205. function onAccept()
  206. {
  207.    if(!validatePrefs())
  208.      return false;
  209.  
  210.    //set enable pref
  211.    prefs.SetBoolPref(autoupdateEnabledString, enabledCheckBox.checked );
  212.    
  213.    //set URL TYPE and value prefs - always to last fetch url - till we have anything else available
  214.    prefs.SetCharPref(autoupdateURLString,crl.lastFetchURL);
  215.    
  216.    var timingTypeId = updateTypeRadio.selectedItem.id;
  217.    var updateTime;
  218.    var dayCnt = (document.getElementById("nextUpdateDay")).value;
  219.    var freqCnt = (document.getElementById("nextUpdateFreq")).value;
  220.    
  221.    if(timingTypeId == "timeBasedRadio"){
  222.      prefs.SetIntPref(autoupdateTimeTypeString,crl.TYPE_AUTOUPDATE_TIME_BASED);
  223.      updateTime = crl.ComputeNextAutoUpdateTime(crl.TYPE_AUTOUPDATE_TIME_BASED, dayCnt);
  224.    } else {
  225.      prefs.SetIntPref(autoupdateTimeTypeString,crl.TYPE_AUTOUPDATE_FREQ_BASED);
  226.      updateTime = crl.ComputeNextAutoUpdateTime(crl.TYPE_AUTOUPDATE_FREQ_BASED, freqCnt);
  227.    }
  228.  
  229.    //alert(updateTime);
  230.    prefs.SetCharPref(autoupdateTimeString,updateTime); 
  231.    prefs.SetCharPref(autoupdateDayCntString,dayCnt);
  232.    prefs.SetCharPref(autoupdateFreqCntString,freqCnt);
  233.  
  234.    //Save Now
  235.    prefs.savePrefFile(null);
  236.    
  237.    certdb.rescheduleCRLAutoUpdate();
  238.    //Close dialog by returning true
  239.    return true;
  240. }
  241.  
  242. function validatePrefs()
  243. {
  244.    var dayCnt = (document.getElementById("nextUpdateDay")).value;
  245.    var freqCnt = (document.getElementById("nextUpdateFreq")).value;
  246.  
  247.    var tmp = parseFloat(dayCnt);
  248.    if(!(tmp > 0.0)){
  249.      alert(bundle.GetStringFromName("crlAutoUpdateDayCntError"));
  250.      return false;
  251.    }
  252.    
  253.    tmp = parseFloat(freqCnt);
  254.    if(!(tmp > 0.0)){
  255.      alert(bundle.GetStringFromName("crlAutoUpdtaeFreqCntError"));
  256.      return false;
  257.    }
  258.    
  259.    return true;
  260. }
  261.